const { pool } = require('../models/db');

// 用户投稿
exports.createArticle = async (req, res) => {
  try {
    const { title, content } = req.body;
    const author_id = req.userId;
    if (!title || !content) {
      return res.json({ success: false, message: '标题和内容不能为空' });
    }
    await pool.execute(
      'INSERT INTO articles (title, content, author_id, status) VALUES (?, ?, ?, ?)',
      [title, content, author_id, 'pending']
    );
    res.json({ success: true, message: '投稿成功，等待审核' });
  } catch (err) {
    res.json({ success: false, message: '投稿失败', error: err.message });
  }
};

// 查询我的投稿
exports.getMyArticles = async (req, res) => {
  try {
    const author_id = req.userId;
    const [rows] = await pool.execute(
      'SELECT * FROM articles WHERE author_id = ? ORDER BY created_at DESC',
      [author_id]
    );
    res.json({ success: true, data: rows });
  } catch (err) {
    res.json({ success: false, message: '查询失败', error: err.message });
  }
}; 